package com.getpebble.android.onboarding.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.onboarding.activity.OnboardingActivity;

public class AppInstallCompleteFragment extends PblBaseFragment {
    public static final String TAG = AppInstallCompleteFragment.class.getSimpleName();
    private Button mContinueButton;
    private TextView mInstallStatus;

    public int getLayoutId() {
        return R.layout.fragment_app_install_complete;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mInstallStatus = (TextView) container.findViewById(R.id.onboarding_app_install_complete_subtitle);
        this.mContinueButton = (Button) container.findViewById(R.id.onboarding_continue_btn);
        this.mContinueButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                MobileAppBehavior.logButtonTapped("continue", "OnboardingMigrationAppInstall");
                Activity activity = AppInstallCompleteFragment.this.getActivity();
                if (activity != null && (activity instanceof OnboardingActivity)) {
                    ((OnboardingActivity) activity).goToNextScreen();
                }
            }
        });
    }

    public void onResume() {
        super.onResume();
        handleArguments();
    }

    private void handleArguments() {
        Bundle args = getArguments();
        if (args != null && args.containsKey("extra_num_apps_installed")) {
            int numInstalled = args.getInt("extra_num_apps_installed");
            int numInstallFailed = args.getInt("extra_num_apps_install_failed");
            String appsInstalled = getResources().getQuantityString(R.plurals.onboarding_num_apps_installed, numInstalled, new Object[]{Integer.valueOf(numInstalled)});
            if (numInstallFailed != 0) {
                appsInstalled = appsInstalled + " " + getResources().getQuantityString(R.plurals.onboarding_num_apps_install_failed, numInstallFailed, new Object[]{Integer.valueOf(numInstallFailed)});
            }
            this.mInstallStatus.setText(appsInstalled);
        }
    }
}
